<template>
    <el-dialog class="schemedialog" title="更换方案" :visible.sync="showSchemeEditForm" :before-close="handleClose" width="30%">
        <div class="confirmContainer" v-show="activeIndex==0">
            <el-col :span="4" class="icon">
                <i class="el-icon-warning"></i>
            </el-col>
            <el-col :span="20">
                <p class="warningText">此操作将停止当前方案（{{getSchemeName}}）绑定的所有服务，是否继续?</p>
            </el-col>          
        </div>
        <div v-show="activeIndex==1">
            <el-form :model="form" ref="ruleForm" :rules="rules" class="customForm">
                <el-form-item label="当前方案" :label-width="formLabelWidth" class="formItem">
                    <span>{{getSchemeName}}</span>
                </el-form-item>
                <el-form-item label="更换方案" :label-width="formLabelWidth" class="formItem" prop="newScheme">
                    <el-select v-model="form.newScheme" @change="handleChanged" placeholder="请选择方案" :loading="loading" loading-text="加载中..." no-data-text="暂无数据">
                        <el-option :label="item.schemeName" :value="item.schemeName" v-for="item in schemes" :key="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button @click="next" v-show="activeIndex==0">确 认</el-button>
            <el-button @click="handleReset" v-show="activeIndex==1">重 置</el-button>
            <el-button type="primary" @click="handleSubmit" v-show="activeIndex==1">提 交</el-button>
        </div>
    </el-dialog>
</template>
<script>
    import util from '../util/util';
    export default {
        props:{
            showSchemeEditForm:{
                type:Boolean,
                required:true,
                default:false
            }
        },
        data(){
            return{
                loading:false,
                active: 0,
                activeIndex: 0,
                formLabelWidth: '100px',
                buttonName: '下一步',
                form: {
                    newScheme: ''
                },
                scheme:'',
                schemes:[],
                rules:{
                    newScheme:[
                        { required:true, message: '请选择需要更换的方案', trigger: 'change' }
                    ]
                }
            }
        },
        computed:{
            getSchemeName:function(){
                return this.$store.state.currentScheme;
            }
        },
        watch:{
            showSchemeEditForm:function(val){
                if(val){
                    this.active = 0;
                    this.activeIndex = 0;
                    this.fetchData();
                }
                
            }
        },
        methods:{
            fetchData(){
                this.loading = true;
                this.$api.scheme.schemeList({page_size:0}).then(response => {
                    this.loading = false;
					if(response.data.code=="6000"){
                        let data = response.data.data.schemes;
                        this.processData(data);                    
                    }else{
                        this.$message({
                            message: util.getErrorInfo(response.data.code),
                            type: 'error'
                        });
                    }
				}).catch(error => {
                    this.loading = false;
                });
                this.$store.dispatch("updateSchemeData", false); 
            },
            processData(data){
                let tmpData = [];
                for(let i in data){
                    let schemeobj = {};
                    if(data[i]["name"] == this.getSchemeName) continue;
                    schemeobj["id"] = i;
                    schemeobj["schemeName"] = data[i]["name"];
                    tmpData.push(schemeobj); 
                }
                this.schemes = tmpData;
            },
            next(){
                this.activeIndex++;
            },
            handleChanged(val){
                this.form.newScheme = val;
            },
            handleClose(){
                this.$emit('update:showSchemeEditForm', false);
                this.handleReset()             
            },
            updateData(){
                let params = {
                    name: this.form.newScheme
                };
                this.$api.scheme.schemeUpdate(params).then(response => {
					if(response.data.code=="6000"){
                        this.$store.dispatch("currentScheme", response.data.data.select);
                        this.$store.dispatch("updateSchemeData", true); 
                        this.$message({
                            type: 'success',
                            message: '修改成功!'
                        });                  
                    }else{
                        this.$message({
                            message: util.getErrorInfo(response.data.code),
                            type: 'error'
                        });
                    }
				}).catch(error => {
                });
            },
            handleSubmit(){
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        //校验成功，提交请求
                        this.updateData();
                        this.$emit('update:showSchemeEditForm', false); 
                        this.handleReset();
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });  
            },
            handleReset(){
                this.$refs.ruleForm.resetFields();
            }
        }
    }
</script>
<style lang="scss" scoped>
    .customForm{
        padding: 0 40px 0 20px;
        .formText{
            margin-bottom: 20px;
        }
        .formItem{
            margin-bottom: 30px;
        }
    }
    .confirmContainer{
        overflow: hidden;
        font-size: 16px;
        .icon{
            text-align: center;
            color: #E6A23C;
            font-size: 2rem;
        }
    }
</style>
<style lang="css" scoped>
.schemedialog >>> .el-dialog{
  background: url("../../static/dialogbg.png") no-repeat ;
  background-size: 100% 100%;
  box-shadow: none;
  padding:2%;
}
.schemedialog >>> .el-dialog__headerbtn{
  right: 12%;
  }
.schemedialog >>> .el-dialog__close{
  color: #fff;
  font-size: 25px;
}
.schemedialog >>> .el-dialog__header{
    text-align: center;
}
</style>